﻿using System;
using System.Collections.Generic;
using System.Text;

namespace daSyntaxAnalyser.SyntaxTree
{
    using daBase.Objects;

    public class TryStatementElement : CompoundStatementElement
    {
        private StatementElement optionblock;

        public StatementElement TryOptionBlock { get { return optionblock; } set { optionblock = value; } }

        public TryStatementElement(TokenInfo StartInfo, List<StatementsElement> Try, StatementElement TryOptionBlock, List<TokenInfo> Comments)
            : base(StartInfo, Try, Comments)
        {
            optionblock = TryOptionBlock;
        }

        public override string ToString()
        {
            string result = string.Format("try\r\n{0}", InnerStatements());
            if (optionblock != null)
                result += optionblock.ToString();
            else result += "end";
            return result;
        }

        protected override IEnumerator<ISyntaxTreeElement> CreateEnumerator()
        {
            IEnumerator<ISyntaxTreeElement> baseEnumerator = base.CreateEnumerator();
            while (baseEnumerator.MoveNext())
                yield return baseEnumerator.Current;
            yield return optionblock;
        }
    }
}
